/** 系统配置 */
import type { AppConfig } from './type'
import { t } from '@/i18n'
import { joinUrls } from '@/utils/generate'
import BackendProvider from './llmProvider/backend'
import OllamaProvider from './llmProvider/ollama'
import GeminiProvider from './llmProvider/gemini'
import DeepSeekProvider from './llmProvider/deepseek'

const config: AppConfig = {
  title: 'Vue3 Ai Scaffold',
  themeColor: '#1677ff',
  darkThemeColor: '#70c0e8',
  defaultLang: () => {
    let lang = import.meta.env.VITE_DEFAULT_LANG
    if (!lang || lang === 'auto') {
      // 自动识别
      lang = navigator.language
      if (['zh-HK', 'zh-TW', 'zh-MO'].includes(lang)) lang = 'zh-Hant'
    }
    return lang
  },
  isMockMode: import.meta.env.VITE_USE_MOCK === 'true',
  llm: {
    // 提供商配置列表
    // 您可以自行实现 provider 并在此处配置即可
    // provider 编写可参考 /src/config/llmProvider/_example.ts
    suppliers: [
      {
        // 内置提供商：backend
        id: 'mock-server',
        provider: BackendProvider,
        name: 'Mock Server',
        alias: {
          'zh-CN': '虚拟服务器',
          'zh-Hant': '虛擬伺服器',
        },
        baseUrl: '/llm',
        isActive: import.meta.env.VITE_USE_MOCK === 'true',
      },
      {
        // 内置提供商：ollama
        id: 'local-ollama',
        provider: OllamaProvider,
        name: 'Ollama',
        baseUrl: import.meta.env.VITE_LLM_OLLAMA_BASE_URL ?? '',
        isActive: !!import.meta.env.VITE_LLM_OLLAMA_BASE_URL,
      },
      {
        // 内置提供商：deepseek
        id: 'deepseek',
        provider: DeepSeekProvider,
        name: 'DeepSeek',
        apiKey: 'YOUR_API_HERE',
        defaultModels: [
          {
            key: 'deepseek-chat',
            type: 'llm',
            name: 'deepseek-chat',
            toolUse: true,
          },
          {
            key: 'deepseek-reasoner',
            type: 'llm',
            name: 'deepseek-reasoner',
            toolUse: true,
          },
        ],
        isActive: false,
      },
      {
        // 内置提供商：gemini
        id: 'gemini',
        provider: GeminiProvider,
        name: 'Gemini',
        apiKey: 'YOUR_API_HERE',
        defaultModels: [
          {
            key: 'gemini-2.5-flash',
            type: 'llm',
            name: 'Gemini 2.5 Flash',
            toolUse: true,
            vision: true,
          },
        ],
        isActive: false,
      },
      // 其它提供商可自定义实现
    ],
  },
  service: {
    whiteList: ['/access/refresh-token', '/access/login'],
    api: {
      url: (reqConfig) => {
        reqConfig.url = reqConfig.url || ''
        // 获取基底api地址，支持从env直接获取
        let baseAPI = import.meta.env.VITE_BASE_API
        if (reqConfig.baseAPI) {
          const envKey = `VITE_${reqConfig.baseAPI}_API`.toUpperCase()
          if (import.meta.env[envKey]) {
            baseAPI = import.meta.env[envKey]
          } else {
            baseAPI = '/'
            window.console.warn(`unknown env api '${envKey}'`)
          }
          // 返回实际服务器地址
          return joinUrls(baseAPI, reqConfig.url)
        }
        // 如果请求地址中包含http头，直接返回
        if (/^(https?:\/\/|\/\/)/.test(reqConfig.url)) {
          return reqConfig.url
        }
        // 是否已开启mock
        if (config.isMockMode) {
          // 返回mock地址（即原地址）
          return reqConfig.url
        }
        // 返回实际服务器地址
        return joinUrls(baseAPI, reqConfig.url)
      },
      response: (res) => {
        if (res) {
          const message = res.data?.message ?? t('account.requestError.nonDelivery')
          if (!res.data?.success) {
            throw new Error(message)
          } else {
            return { message, data: res.data?.data }
          }
        } else {
          return { message: t('account.requestError.nonDelivery'), data: null }
        }
      },
    },
  },
}

export default config
